﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LEDScreenLinkTool.Model
{
    public class SaveScreenPictureOper
    {
        /// <summary>
        /// 根据灯板位置生成整屏预览图文件
        /// </summary>
        /// <param name="bodyWidth"></param>
        /// <param name="bodyHeight"></param>
        /// <param name="moduleList"></param>
        /// <param name="picPath"></param>
        /// <returns></returns>
        public bool SavePicture(double bodyWidth,double bodyHeight,List<ModuleGridItem> moduleList, string picPath)
        {
            bool rst = false;
            try
            {
                Bitmap bmp = new Bitmap((int)bodyWidth, (int)bodyHeight);
                Graphics g = Graphics.FromImage(bmp);
                g.Clear(Color.Aqua);

                char screenNoA = 'A';
                char screenNoB = 'B';
                char screenNoC = 'C';
                foreach (var module in moduleList)
                {
                    g.DrawRectangle(new Pen(Color.Black), new Rectangle(new Point((int)module.GridLeft, (int)module.GridTop), new Size((int)module.GridWidth, (int)module.GridHeight)));
                    switch (module.ModuleType)
                    {
                        case EModuleType.P4_2x2:
                    g.DrawString(screenNoA.ToString(), new Font("Times New Roman", 16), new SolidBrush(Color.Red), (int)module.GridLeft, (int)module.GridTop);
                            break;
                        case EModuleType.P4_2x1:
                            g.DrawString(screenNoB.ToString(), new Font("Times New Roman", 16), new SolidBrush(Color.Red), (int)module.GridLeft, (int)module.GridTop);
                            break;
                        case EModuleType.P4_3x1:
                            g.DrawString(screenNoC.ToString(), new Font("Times New Roman", 16), new SolidBrush(Color.Red), (int)module.GridLeft, (int)module.GridTop);
                            break;
                    }

                }
                g.SmoothingMode = SmoothingMode.AntiAlias;
                bmp.Save(picPath, ImageFormat.Png);
                rst = true;

            }
            catch (Exception ex)
            {
                rst = false;
            }
            return rst;
        }
    }
}
